﻿using UnityEngine;
using System.Collections;

public class InterstitialShowScript : MonoBehaviour {

	private TextMesh debugText;
	private float delayTimer = 1f;
	private bool startedDelay = false;
	GoogleMobileAds.Api.InterstitialAd googleAd;

	// Use this for initialization
	void Start () {
		this.debugText = (TextMesh)GameObject.Find("DebugText").GetComponent(typeof(TextMesh));
		if (Application.platform == RuntimePlatform.WP8Player || 
		    Application.platform == RuntimePlatform.MetroPlayerX86 ||
		    Application.platform == RuntimePlatform.MetroPlayerARM ||
		    Application.platform == RuntimePlatform.MetroPlayerX64
		    ) {
			PubCenterPlugin.PubCenterInteraction.ShowInterstitialAds();
			PubCenterPlugin.PubCenterInteraction.AdsDismissed += CloseAds;
		}
		else if (Application.platform == RuntimePlatform.Android) {
			try {
				googleAd = new GoogleMobileAds.Api.InterstitialAd("ca-app-pub-7664787518361934/5485529203");
				googleAd.AdLoaded += HandleAdLoaded;
				googleAd.AdFailedToLoad += HandleAdFailedToLoad;
				googleAd.AdClosed += HandleAdClosed;
				GoogleMobileAds.Api.AdRequest req = new GoogleMobileAds.Api.AdRequest.Builder().AddKeyword("game").Build();
				googleAd.LoadAd(req);
			}
			catch(System.Exception ex) {
				GameMaintainerSingleton.Instance.DebugText = ex.ToString();
				this.debugText.text = ex.ToString();
			}
			//GameMaintainerSingleton.Instance.InitChartBoost();
			//GameMaintainerSingleton.Instance.ChartBoostAdReady += HandleChartBoostAdReady;
			//GameMaintainerSingleton.Instance.ChartBoostAdFinished += HandleChartBoostAdFinished;
			/*
			if (GameMaintainerSingleton.Instance.HasAdColonyCachedAd) {
				this.debugText.text = "Has AdColony";
				GameMaintainerSingleton.Instance.AdColonyAdFinished += HandleAdColonyAdFinished;
				AdColony.ShowVideoAd();
			}
			else if (GameMaintainerSingleton.Instance.HasChartBoostCachedAd) {
				this.debugText.text = "Has ChartBoost";
				GameMaintainerSingleton.Instance.ChartBoostAdFinished += HandleChartBoostAdFinished;
				Chartboost.CBBinding.showInterstitial(null);
			}
			else {
				this.debugText.text = "No Ads Available";
				this.startedDelay = true;
			}
			*/
		}
	}

	void HandleAdFailedToLoad (object sender, GoogleMobileAds.Api.AdFailedToLoadEventArgs e)
	{
		this.debugText.text = "Failed to load: " + e.Message;
		this.startedDelay = true;
	}

	void HandleAdClosed (object sender, System.EventArgs e)
	{
		this.startedDelay = true;
	}

	void HandleAdLoaded (object sender, System.EventArgs e)
	{
		googleAd.Show();
	}

	void HandleChartBoostAdReady (object sender, System.EventArgs e)
	{
		//GameMaintainerSingleton.Instance.ShowChartBoostAd();
	}

	void HandleChartBoostAdFinished (object sender, System.EventArgs e)
	{

		//this.debugText.text = "ChartBoost Finished";
		this.startedDelay = true;
	}

	void HandleAdColonyAdFinished (object sender, System.EventArgs e)
	{
		//this.debugText.text = "AdColony Finished";
		this.startedDelay = true;
	}

	void OnApplicationPause(bool paused) {
		// Manage Chartboost plugin lifecycle
		if (Application.platform == RuntimePlatform.Android) {
//#if UNITY_ANDROID
//			CBBinding.pause(paused);
//#endif
		}
	}

	void CloseAds(object sender, System.EventArgs e) {
		Application.LoadLevel(0);
	}
	
	// Update is called once per frame
	void Update () {
		if (this.startedDelay) {
			this.delayTimer -= Time.deltaTime;
			if (this.delayTimer <= 0f) {
				startedDelay = false;
				Application.LoadLevel(0);
			}
		}
		if (Input.GetKey(KeyCode.Escape)) {
		}
	}

}
